package com.example.shop_2207.search

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.LayoutInflater
import android.widget.TextView
import androidx.appcompat.widget.SearchView
import androidx.core.view.isVisible
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.shop_2207.R
import com.example.shop_2207.adapter.GoodsAdapter
import com.example.shop_2207.database.MyDbUtils
import com.example.shop_2207.databinding.ActivitySearchBinding
import com.example.shop_2207.entity.GoodsEntity
import com.example.shop_2207.entity.HistoryEntity
import com.example.shop_2207.mvvm.BaseActivity

@Route(path = "/app/SearchActivity")
class SearchActivity : BaseActivity<ActivitySearchBinding,SearchViewModel>() {

    val list = mutableListOf<GoodsEntity>()
    lateinit var goodsAdapter: GoodsAdapter
    override fun getLayout(): Int = R.layout.activity_search

    override fun getViewModel(): Class<SearchViewModel>  = SearchViewModel::class.java

    override fun initView() {
        goodsAdapter = GoodsAdapter(list)
        binding.rvSearch.adapter = goodsAdapter
        binding.rvSearch.layoutManager = LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

        //实现调用查询操作
        binding.search.setOnQueryTextListener(object: SearchView.OnQueryTextListener{
            override fun onQueryTextSubmit(query: String?): Boolean {
                //点击提交   可以是回车 实现提交   点击搜索图片也能实现提交查询
                if(query!=null){
                    viewModel.search(query)
                    //保存历史记录
                    MyDbUtils.getDb().getHisDao().insertHis(HistoryEntity(query))
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                //文本实时改变进行查询
                /*if(newText!=null){
                    viewModel.search(newText)
                }*/
                return true
            }
        })
        //返回图标的点击事件
        binding.back.setOnClickListener {
            finish()
        }
        //历史搜索记录展示
        //查询数据库中数据
        val hislist = MyDbUtils.getDb().getHisDao().queryHis()
        //将历史记录添加到chipgroup中
        if(hislist.isNotEmpty()){
            binding.chipGroup.removeAllViews()//先清空之前的历史记录
            hislist.forEach {
                val key = it.key
                //获取chip的布局
                val view = LayoutInflater.from(this).inflate(R.layout.item_chip, null)
                //设置chip的内容
                view.findViewById<TextView>(R.id.chip).text = key
                //添加到chipgroup中
                binding.chipGroup.addView(view)
                //点击chip可直接再次查询
                view.setOnClickListener {
                    binding.search.setQuery(key,true)
                }
            }
        }
    }

    override fun initData() {
        viewModel.searchSuccess.observe(this){
            //查询成功后，隐藏历史搜索记录
            binding.chipGroup.isVisible = false
            if(it.isNotEmpty()){
                list.clear()//防止出现上次查询的数据
                list.addAll(it)
                goodsAdapter.notifyDataSetChanged()
            }else{
                ToastUtils.showLong("为查询到符合条件的数据")
            }
        }
        viewModel.searchFail.observe(this){
            ToastUtils.showLong(it)
        }
    }

}